import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:mall/core/models/bank.dart';
import 'package:mall/core/providers/pages/mine/manage/withdraw_create_model.dart';
import 'package:mall/core/providers/view_model.dart';
import 'package:mall/screens/manage/bank_page.dart';
import 'package:mall/screens/manage/withdraw_account_page.dart';
import 'package:provider/provider.dart';

class WithdrawEditPage extends StatelessWidget {
  static const routeName = '/WithdrawEditPage';

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (context) => WithdrawCreateModel(Provider.of(context, listen: false)),
      child: Scaffold(
        appBar: AppBar(
          title: Text('创建店铺提现'),
        ),
        body: Column(
          children: <Widget>[
            Expanded(
              child: _ListWidget(),
            ),
            _BottomWidget(),
          ],
        ),
      ),
    );
  }
}

class _ListWidget extends StatelessWidget {
  const _ListWidget({
    Key key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    WithdrawCreateModel model = Provider.of(context);
    return ListView(
      children: <Widget>[
        _ListSelectWidget(
          title: '选择提现店铺账户类型',
          value: model.account?.value ?? '',
          function: () => _onAccountClicked(context),
        ),
        _ListSelectWidget(
          title: '选择提现到账账户',
          value: model.bank?.bankCardId ?? '',
          function: () => _onBankClicked(context),
        ),
        _ListEditWidget(),
      ],
    );
  }

  void _onAccountClicked(BuildContext context) async {
    var result = await Navigator.of(context).pushNamed(WithdrawAccountPage.routeName);
    if (result is MapEntry) {
      WithdrawCreateModel model = Provider.of(context, listen: false);
      model.account = result;
    }
  }

  void _onBankClicked(BuildContext context) async {
    var result = await Navigator.of(context).pushNamed(BankPage.routeName, arguments: true);
    if (result is Bank) {
      WithdrawCreateModel model = Provider.of(context, listen: false);
      model.bank = result;
    }
  }
}

class _ListSelectWidget extends StatelessWidget {
  final String title;
  final String value;
  final Function function;

  const _ListSelectWidget({
    Key key,
    @required this.title,
    @required this.value,
    @required this.function,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      child: Container(
        color: Colors.white,
        height: 45,
        child: Column(
          children: <Widget>[
            Expanded(
              child: Row(
                children: <Widget>[
                  SizedBox(width: 12),
                  Expanded(
                    child: Text(
                      title,
                      style: TextStyle(color: Color(0xFF252525)),
                    ),
                  ),
                  Text(
                    value,
                    style: TextStyle(color: Color(0xFFAEAEAE), fontSize: 12),
                  ),
                  Icon(Icons.keyboard_arrow_right, color: Color(0xFFAEAEAE)),
                  SizedBox(width: 12),
                ],
              ),
            ),
            Divider(
              indent: 12,
              endIndent: 12,
              height: 1,
              color: Color(0xFFD6D6D6),
            )
          ],
        ),
      ),
      onTap: () => function(),
    );
  }
}

class _ListEditWidget extends StatelessWidget {
  const _ListEditWidget({
    Key key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        color: Colors.white,
      ),
      height: 45,
      child: Row(
        children: <Widget>[
          SizedBox(width: 12),
          Text(
            '提现额度：',
            style: TextStyle(
              color: Color(0xFF252525),
            ),
          ),
          Expanded(
            child: TextField(
              decoration: InputDecoration(
                hintText: '输入提现额度',
                hintStyle: TextStyle(color: Color(0xFFAEAEAE), fontSize: 14),
                border: InputBorder.none,
              ),
              style: TextStyle(color: Color(0xFFAEAEAE), fontSize: 14),
              keyboardType: TextInputType.number,
              onChanged: (value) => _onTextChanged(context, value),
            ),
          ),
          SizedBox(width: 12),
        ],
      ),
    );
  }

  void _onTextChanged(BuildContext context, String value) {
    WithdrawCreateModel model = Provider.of(context, listen: false);
    model.amount = value;
  }
}

class _BottomWidget extends StatelessWidget {
  const _BottomWidget({
    Key key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    WithdrawCreateModel model = Provider.of(context);
    return Container(
      width: double.maxFinite,
      margin: EdgeInsets.all(16),
      child: model.viewState == ViewState.busy
          ? RaisedButton(
              onPressed: null,
              child: SizedBox(
                width: 12,
                height: 12,
                child: CircularProgressIndicator(),
              ),
            )
          : RaisedButton(
              onPressed: () => _onButtonClicked(context),
              child: Text('提交'),
            ),
    );
  }

  void _onButtonClicked(BuildContext context) async {
    WithdrawCreateModel model = Provider.of(context, listen: false);
    ViewState viewState = await model.withdrawCreate();
    switch (viewState) {
      case ViewState.error:
        EasyLoading.showToast(model.errorMessage);
        break;
      default:
        Navigator.of(context).pop(true);
        break;
    }
  }
}
